package com.szholly.plug.safe.entity.role;

import org.springframework.stereotype.Component;

import com.szholly.utils.util.DateUtils;
import com.szholly.data.general.ITable;
import com.szholly.data.general.RowBase;
import com.szholly.data.general.interceptor.DbEditInterceptorClass;
import com.szholly.data.general.interceptor.DbEditType;
import com.szholly.data.general.interceptor.InterceptorRegister;

/**
 * 定义角色委托变更数据拦截接口，用于清除角色委托缓存
 */
@Component
public class RoleWtEditInterceptor extends DbEditInterceptorClass {
	
	/**
	 * 注册拦截接口
	 */
	static {
		RoleWtEditInterceptor interceptor = new RoleWtEditInterceptor();
		InterceptorRegister.RegDbInterceptor(interceptor);
	}
	
	public RoleWtEditInterceptor(){
		this.regTableName(RoleChangeService.TABLE_NAME);
	}

	@Override
	public void BeforeDbEditOption(ITable table, Object editEntitys,
			DbEditType editType) {
		// 如果是新增，并且状态值为空，处理默认值
		if(editType == DbEditType.Insert && editEntitys instanceof RowBase){
			RowBase row = (RowBase)editEntitys;
			if(row.getValue(RoleChangeEntity.FIELD_ISUSE) == null){
				row.setValue(RoleChangeEntity.FIELD_ISUSE, RoleWtDic.useing);
			}
			if(row.getValue(RoleChangeEntity.FIELD_DJ_BEGINTIME) == null){
				row.setValue(RoleChangeEntity.FIELD_DJ_BEGINTIME, DateUtils.convertDateTimeToString(null));
			}
		}
	}

	@Override
	public void DbEditOptionException(ITable table, Object editEntitys,
			DbEditType editType, Exception ex) {
	}

	@Override
	public void AfterDbEditOption(ITable table, Object editEntitys,
			DbEditType editType) {
		RoleInitHelper.ReInitWtRoleData();
	}

	@Override
	public String getInterceptorName() {
		return "定义角色委托变更数据拦截接口，用于清除角色委托缓存";
	}
}
